package com.gpframework.module.wages.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.common.exception.CustomException;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.wages.domain.GzPersonnelFoundation;
import com.gpframework.module.wages.domain.GzSalaryLevelTransferWarehouseWorkers;
import com.gpframework.module.wages.domain.GzWarehouseWorkerSalaryLevel;
import com.gpframework.module.wages.domain.vo.*;
import com.gpframework.module.wages.mapper.GzPersonnelFoundationMapper;
import com.gpframework.module.wages.service.IGzPersonnelFoundationService;
import com.gpframework.module.wages.service.IGzSalaryLevelTransferWarehouseWorkersService;
import com.gpframework.module.wages.service.IGzWarehouseWorkerSalaryLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName GzPersonnelFoundation
 * @Author gpframework
 * @Date 2024-01-18
 * @Version 1.0.0
 * @Description 人员基础Service业务层处理
 */
@Service
public class GzPersonnelFoundationServiceImpl extends ServiceImpl<GzPersonnelFoundationMapper, GzPersonnelFoundation> implements IGzPersonnelFoundationService {


    @Autowired
    private IGzPersonnelFoundationService iGzPersonnelFoundationService;

    @Autowired
    private IGzSalaryLevelTransferWarehouseWorkersService iGzSalaryLevelTransferWarehouseWorkersService;

    @Autowired
    private IGzWarehouseWorkerSalaryLevelService iGzWarehouseWorkerSalaryLevelService;


    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2024-01-18
     * @Param [lqw]
     * @Return List<GzPersonnelFoundationVo>
     */
    @Override
    public List<GzPersonnelFoundationVo> listVo(QueryWrapper<GzPersonnelFoundation> lqw) {
        return this.baseMapper.listVo(lqw);
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2024-01-18
     * @Param [id]
     * @Return GzPersonnelFoundationVo
     */
    @Override
    public GzPersonnelFoundationVo getVoById(Long id) {
        return this.baseMapper.getVo(id);
    }

    @Override
    public List<SalarySummaryVo> salarySummary(String month, String deptName) {
        return this.baseMapper.salarySummary(month, deptName);
    }

    @Override
    public List<GzPersonnelVersionVo> personnelVersion(String month, String deptName, String name) {
        return this.baseMapper.personnelVersion(month, deptName, name);
    }


    @Override
    public List<GzTransferVo> transfeSummary(String month, String deptName) {
        return this.baseMapper.transfeSummary(month, deptName);
    }

    @Override
    public List<GzWarehousePersonnelVo> warehouseSummary(String month, String deptName) {
        return this.baseMapper.warehouseSummary(month, deptName);
    }

    @Override
    public String importListNote(List<GzPersonnelFoundationVo> excelList, boolean isUpdateSupport) {
        if (StringUtils.isNull(excelList) || excelList.size() == 0) {
            throw new CustomException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (GzPersonnelFoundationVo gzPersonnelFoundationVo : excelList) {
            try {
                //查询数据
                GzPersonnelFoundation gzPersonnelFoundation = iGzPersonnelFoundationService.getOne(new QueryWrapper<GzPersonnelFoundation>().eq("name", gzPersonnelFoundationVo.getName()).eq("del_status", YesNoEnum.NO.getKey()));
                if (StringUtils.isNull(gzPersonnelFoundation)) {
                    //新增的时候 删除旧数据 名字相同的数据---
                    iGzPersonnelFoundationService.remove(new QueryWrapper<GzPersonnelFoundation>().eq("name", gzPersonnelFoundationVo.getName()).eq("del_status", YesNoEnum.NO.getKey()));
                    //导入办公室和档口
                    if (gzPersonnelFoundationVo.getDepartmentType() == 1) {
                        GzPersonnelFoundation personnelFoundation = Convert.convert(new TypeReference<GzPersonnelFoundation>() {
                        }, gzPersonnelFoundationVo);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、姓名 " + personnelFoundation.getName() + " 导入成功");
                        iGzPersonnelFoundationService.save(personnelFoundation);
                    }

                    //导入中转 或者 仓库
                    if (gzPersonnelFoundationVo.getSubclassIevel() != null && gzPersonnelFoundationVo.getSalaryLevel() != null) {
                        // 中转仓员工资 基本工资数据
                        if (gzPersonnelFoundationVo.getDepartmentType() == 2) {
                            GzPersonnelFoundation personnelFoundation = Convert.convert(new TypeReference<GzPersonnelFoundation>() {
                            }, gzPersonnelFoundationVo);

                            GzSalaryLevelTransferWarehouseWorkers gzSalaryLevelTransferWarehouseWorkers = iGzSalaryLevelTransferWarehouseWorkersService.getOne(new LambdaQueryWrapper<GzSalaryLevelTransferWarehouseWorkers>()
                                    .eq(GzSalaryLevelTransferWarehouseWorkers::getCategoryLevel, gzPersonnelFoundationVo.getSalaryLevel())
                                    .eq(GzSalaryLevelTransferWarehouseWorkers::getSubclassLevel, gzPersonnelFoundationVo.getSubclassIevel())
                                    .eq(GzSalaryLevelTransferWarehouseWorkers::getDelStatus, YesNoEnum.NO.getKey()));

                            personnelFoundation.setBasicSalary(gzSalaryLevelTransferWarehouseWorkers.getBaseSalary());
                            //岗位工资
                            personnelFoundation.setPostWage(gzSalaryLevelTransferWarehouseWorkers.getAllowance());
                            personnelFoundation.setManagementAllowance(gzSalaryLevelTransferWarehouseWorkers.getManagementAllowance());
                            personnelFoundation.setHouseaLlowance(gzSalaryLevelTransferWarehouseWorkers.getHouseaLlowance());
                            personnelFoundation.setServiceExperienceAward(gzSalaryLevelTransferWarehouseWorkers.getSeniorityPay());

                            personnelFoundation.setAchievementBonus(gzSalaryLevelTransferWarehouseWorkers.getAchievementBonus());
                            personnelFoundation.setEvaluationCriteriaBonus(gzSalaryLevelTransferWarehouseWorkers.getEvaluationCriteriaBonus());
                            personnelFoundation.setSocialSecuritySubsidies(gzSalaryLevelTransferWarehouseWorkers.getSocialSecuritySubsidies());
                            // 加班费 全勤奖
                            personnelFoundation.setFullAttendanceBonus(gzSalaryLevelTransferWarehouseWorkers.getFullAttendanceBonus());
                            personnelFoundation.setOvertimePay(gzSalaryLevelTransferWarehouseWorkers.getOvertimePay());
                            successNum++;
                            successMsg.append("<br/>" + successNum + "、姓名 " + personnelFoundation.getName() + " 导入成功");
                            iGzPersonnelFoundationService.save(personnelFoundation);
                        }

                        //仓储中心员工 基本工资数据
                        if (gzPersonnelFoundationVo.getDepartmentType() == 3) {
                            GzPersonnelFoundation personnelFoundation = Convert.convert(new TypeReference<GzPersonnelFoundation>() {
                            }, gzPersonnelFoundationVo);

                            GzWarehouseWorkerSalaryLevel gzWarehouseWorkerSalaryLevel = iGzWarehouseWorkerSalaryLevelService.getOne(new LambdaQueryWrapper<GzWarehouseWorkerSalaryLevel>()
                                    .eq(GzWarehouseWorkerSalaryLevel::getCategoryLevel, gzPersonnelFoundationVo.getSalaryLevel())
                                    .eq(GzWarehouseWorkerSalaryLevel::getSubclassLevel, gzPersonnelFoundationVo.getSubclassIevel())
                                    .eq(GzWarehouseWorkerSalaryLevel::getDelStatus, YesNoEnum.NO.getKey()));
                            personnelFoundation.setBasicSalary(gzWarehouseWorkerSalaryLevel.getBaseSalary());
                            //岗位工资
                            personnelFoundation.setPostWage(gzWarehouseWorkerSalaryLevel.getAllowance());
                            personnelFoundation.setManagementAllowance(gzWarehouseWorkerSalaryLevel.getManagementAllowance());
                            personnelFoundation.setHouseaLlowance(gzWarehouseWorkerSalaryLevel.getHouseaLlowance());
                            personnelFoundation.setServiceExperienceAward(gzWarehouseWorkerSalaryLevel.getSeniorityPay());

                            personnelFoundation.setPerformanceCoefficient(gzWarehouseWorkerSalaryLevel.getPerformanceCoefficient());
                            personnelFoundation.setAchievementBonus(gzWarehouseWorkerSalaryLevel.getAchievementBonus());
                            personnelFoundation.setEvaluationCriteriaBonus(gzWarehouseWorkerSalaryLevel.getEvaluationCriteriaBonus());
                            personnelFoundation.setSocialSecuritySubsidies(gzWarehouseWorkerSalaryLevel.getSocialSecuritySubsidies());
                            // 加班费 全勤奖
                            personnelFoundation.setFullAttendanceBonus(gzWarehouseWorkerSalaryLevel.getFullAttendanceBonus());
                            personnelFoundation.setOvertimePay(gzWarehouseWorkerSalaryLevel.getOvertimePay());
                            successNum++;
                            successMsg.append("<br/>" + successNum + "、姓名 " + personnelFoundation.getName() + " 导入成功");
                            iGzPersonnelFoundationService.save(personnelFoundation);

                        }

                    }
                } else if (isUpdateSupport) {
                    //更新办公室和档口
                    if (gzPersonnelFoundationVo.getDepartmentType() == 1) {
                        GzPersonnelFoundation personnelFoundation = Convert.convert(new TypeReference<GzPersonnelFoundation>() {
                        }, gzPersonnelFoundationVo);
                        personnelFoundation.setId(gzPersonnelFoundation.getId());
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、姓名 " + personnelFoundation.getName() + " 更新成功");
                        iGzPersonnelFoundationService.updateById(personnelFoundation);
                    }

                    //更新中转 或者 仓库
                    if (gzPersonnelFoundationVo.getSubclassIevel() != null || gzPersonnelFoundationVo.getSalaryLevel() != null) {
                        // 中转仓员工资 基本工资数据
                        if (gzPersonnelFoundation.getDepartmentType() == 2) {
                            GzPersonnelFoundation personnelFoundation = Convert.convert(new TypeReference<GzPersonnelFoundation>() {
                            }, gzPersonnelFoundationVo);

                            GzSalaryLevelTransferWarehouseWorkers gzSalaryLevelTransferWarehouseWorkers = iGzSalaryLevelTransferWarehouseWorkersService.getOne(new LambdaQueryWrapper<GzSalaryLevelTransferWarehouseWorkers>()
                                    .eq(GzSalaryLevelTransferWarehouseWorkers::getCategoryLevel, gzPersonnelFoundationVo.getSalaryLevel())
                                    .eq(GzSalaryLevelTransferWarehouseWorkers::getSubclassLevel, gzPersonnelFoundationVo.getSubclassIevel())
                                    .eq(GzSalaryLevelTransferWarehouseWorkers::getDelStatus, YesNoEnum.NO.getKey()));

                            personnelFoundation.setBasicSalary(gzSalaryLevelTransferWarehouseWorkers.getBaseSalary());
                            //岗位工资
                            personnelFoundation.setPostWage(gzSalaryLevelTransferWarehouseWorkers.getAllowance());
                            personnelFoundation.setManagementAllowance(gzSalaryLevelTransferWarehouseWorkers.getManagementAllowance());
                            personnelFoundation.setHouseaLlowance(gzSalaryLevelTransferWarehouseWorkers.getHouseaLlowance());
                            personnelFoundation.setServiceExperienceAward(gzSalaryLevelTransferWarehouseWorkers.getSeniorityPay());

                            personnelFoundation.setAchievementBonus(gzSalaryLevelTransferWarehouseWorkers.getAchievementBonus());
                            personnelFoundation.setEvaluationCriteriaBonus(gzSalaryLevelTransferWarehouseWorkers.getEvaluationCriteriaBonus());
                            personnelFoundation.setSocialSecuritySubsidies(gzSalaryLevelTransferWarehouseWorkers.getSocialSecuritySubsidies());
                            // 加班费 全勤奖
                            personnelFoundation.setFullAttendanceBonus(gzSalaryLevelTransferWarehouseWorkers.getFullAttendanceBonus());
                            personnelFoundation.setOvertimePay(gzSalaryLevelTransferWarehouseWorkers.getOvertimePay());
                            personnelFoundation.setId(gzPersonnelFoundation.getId());
                            successNum++;
                            successMsg.append("<br/>" + successNum + "、姓名 " + personnelFoundation.getName() + " 更新成功");
                            iGzPersonnelFoundationService.updateById(personnelFoundation);
                        }

                        //仓储中心员工 基本工资数据
                        if (gzPersonnelFoundationVo.getDepartmentType() == 3) {
                            GzPersonnelFoundation personnelFoundation = Convert.convert(new TypeReference<GzPersonnelFoundation>() {
                            }, gzPersonnelFoundationVo);

                            GzWarehouseWorkerSalaryLevel gzWarehouseWorkerSalaryLevel = iGzWarehouseWorkerSalaryLevelService.getOne(new LambdaQueryWrapper<GzWarehouseWorkerSalaryLevel>()
                                    .eq(GzWarehouseWorkerSalaryLevel::getCategoryLevel, gzPersonnelFoundationVo.getSalaryLevel())
                                    .eq(GzWarehouseWorkerSalaryLevel::getSubclassLevel, gzPersonnelFoundationVo.getSubclassIevel())
                                    .eq(GzWarehouseWorkerSalaryLevel::getDelStatus, YesNoEnum.NO.getKey()));
                            personnelFoundation.setBasicSalary(gzWarehouseWorkerSalaryLevel.getBaseSalary());
                            //岗位工资
                            personnelFoundation.setPostWage(gzWarehouseWorkerSalaryLevel.getAllowance());
                            personnelFoundation.setManagementAllowance(gzWarehouseWorkerSalaryLevel.getManagementAllowance());
                            personnelFoundation.setHouseaLlowance(gzWarehouseWorkerSalaryLevel.getHouseaLlowance());
                            personnelFoundation.setServiceExperienceAward(gzWarehouseWorkerSalaryLevel.getSeniorityPay());

                            personnelFoundation.setPerformanceCoefficient(gzWarehouseWorkerSalaryLevel.getPerformanceCoefficient());
                            personnelFoundation.setAchievementBonus(gzWarehouseWorkerSalaryLevel.getAchievementBonus());
                            personnelFoundation.setEvaluationCriteriaBonus(gzWarehouseWorkerSalaryLevel.getEvaluationCriteriaBonus());
                            personnelFoundation.setSocialSecuritySubsidies(gzWarehouseWorkerSalaryLevel.getSocialSecuritySubsidies());
                            // 加班费 全勤奖
                            personnelFoundation.setFullAttendanceBonus(gzWarehouseWorkerSalaryLevel.getFullAttendanceBonus());
                            personnelFoundation.setOvertimePay(gzWarehouseWorkerSalaryLevel.getOvertimePay());
                            personnelFoundation.setId(gzPersonnelFoundation.getId());
                            successNum++;
                            successMsg.append("<br/>" + successNum + "、姓名 " + personnelFoundation.getName() + " 更新成功");
                            iGzPersonnelFoundationService.updateById(personnelFoundation);
                        }
                    }

                }
                else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、姓名 " + gzPersonnelFoundationVo.getName() + " 已存在");

                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、姓名 " + gzPersonnelFoundationVo.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }

        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }

        return successMsg.toString();
    }


    @Override
    public List<GzOfficeSalaryVo> gzOfficeSalarySummary(String month, String deptName) {
        return this.baseMapper.gzOfficeSalarySummary(month, deptName);
    }

}
